package com.ckd.flyingtrip.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.adapter.GridViewMaicheiAdapter;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.javabean.CityBean;
import com.ckd.flyingtrip.javabean.MaiCheBean;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.MyListView;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 * 新车
 * */
public class XincheActivity extends BaseActivity {
    @BindView(R.id.cd_title_img)
    ImageView cdTitleImg;
    @BindView(R.id.cd_close)
    TextView cdClose;
    @BindView(R.id.cd_title_btn)
    TextView cdTitleBtn;
    @BindView(R.id.listview)
    MyListView listview;
    @BindView(R.id.bottom_img_s1)
    ImageView bottomImgS1;
    @BindView(R.id.btn_xinche)
    LinearLayout btnXinche;
    @BindView(R.id.btn_ershou)
    LinearLayout btnErshou;
    private GridViewMaicheiAdapter gridViewMaicheiAdapter;
    private List<MaiCheBean> list = new ArrayList<>();
    private String type = "0";

    private CityBean cityBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xinche);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        Tools.title_info(this, "租车买车");
        cityBean = (CityBean) getIntent().getSerializableExtra(Constants.POT_DETAIL);
        volleyCar(type);

    }

    /*
     * 车辆列表
     * */
    public void volleyCar(String type) {
        list.clear();
        final Gson mGson = new Gson();
        String url = Constants.MAICHEXINXI;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("车辆返回", s);
                //s为请求返回的字符串数据
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        JSONArray dataInfo = json.getJSONArray("data");
                        for (int i = 0; i < dataInfo.length(); i++) {
                            MaiCheBean maiCheBean = mGson.fromJson(dataInfo.getString(i), MaiCheBean.class);
                            list.add(maiCheBean);
                        }
                        gridViewMaicheiAdapter = new GridViewMaicheiAdapter(XincheActivity.this, list);
                        listview.setAdapter(gridViewMaicheiAdapter);
                        gridViewMaicheiAdapter.notifyDataSetChanged();
                    } else {
                        Log.i("站好", "账号");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {//创建请求参数

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("cityId", Constants.CITYID);
                    json_url.put("siteNum", cityBean.getSiteNum());
//                    carType：车类型 1新车  2 二手车
                    json_url.put("carType", type);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    @OnClick({R.id.btn_xinche, R.id.btn_ershou, R.id.btn_zhulin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_xinche:
                type = "1";
                volleyCar(type);
                break;
            case R.id.btn_ershou:
                type = "2";
                volleyCar(type);
                break;
            case R.id.btn_zhulin:
                type = "0";
                volleyCar(type);
                break;
        }
    }
}
